package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.project.bean.BuyCarBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.mapper.IBuyCarMapper;
import com.project.mapper.IProductMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IBuyCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyCarServiceImp implements IBuyCarService {
    @Autowired
    private IBuyCarMapper buyCarMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IUserMapper userMapper;
    @Override
    public List<BuyCarBean> findById(Integer loginUserId) {
        QueryWrapper<BuyCarBean> query = new QueryWrapper<>();
        query.eq("fk_userId",loginUserId);

        List<BuyCarBean> buyCarBeans = buyCarMapper.selectList(query);
        System.err.println(buyCarBeans);
        for (BuyCarBean buyCarBean : buyCarBeans) {
            QueryWrapper<ProductBean> query1 = new QueryWrapper<>();
            query1.eq("pk_productId",buyCarBean.getProductId());
            buyCarBean.setProductBeanInBuyCar(productMapper.selectOne(query1));

            QueryWrapper<UserBean> query2 = new QueryWrapper<>();
            query2.eq("pk_userId",buyCarBean.getUserId());
            buyCarBean.setUserBeanInBuyCar(userMapper.selectOne(query2));
        }
        return buyCarBeans;
    }

    @Override
    public void updateById(Integer buyCarId, Integer newPrice, Integer newNum) {
        BuyCarBean buyCarBean = buyCarMapper.selectById(buyCarId);
        if (buyCarBean != null) {
            buyCarBean.setNum(newNum);
            buyCarBean.setPrice(newPrice);
            buyCarMapper.updateById(buyCarBean);
        }
    }

    @Override
    public void del(Integer buyCarId) {
        buyCarMapper.deleteById(buyCarId);
    }
}
